01. Spring Boot Exception

035ND C01 L04 A01 SPRINGBOOT EXCEPTION

Spring Boot Exception Handling

035ND C01 L04 A02 EXCEPTION HANDLING

There are five types of situations we can handle Spring Boot exceptions rather than showing error pages like 404 or 500.

  1. Create custom error page.

  2. @ExceptionHandler.

  3. @ControllerAdvice + @ExceptionHandler.

  4. Configure SimpleMappingExceptionResolver class.

  5. Custom HandlerExceptionResolver class.

Let’s learn these one by one.

  1. Let’s create a new project, named spring-boot-exception with web, thymeleaf and devTool as dependency.
  2. Download, unzip and import as always.
  3. Create a UserController inside controller directory.
@Controller
public class UserController {

   @RequestMapping("/add")
   public String add() {
       int num = 10 / 0;
       return "add";
   }
}

Create an empty add.html inside resources/templates directory.

Run the application and goto http://localhost:8080/add

035ND C01 L04 A03 SPRINGBOOT EXAMPLE

If everything is setup correctly, you should see something like below

There was an unexpected error (type=Internal Server Error, status=500).
/ by zero
java.lang.ArithmeticException: / by zero

The page you are looking at is the spring boot default error handling page. But what if we want to show our own custom page.

What we can do is to add a error.html page in resources/template folder

(create a error.html in IDE)

It will be something like:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
   <meta charset="UTF-8">
   <title>Custom Error Page</title>
</head>
<body>
   <h3>Error Page</h3>
</body>
</html>

Because we are using devTool already, once you add the error page, you can just refresh localhost:8080/add, and you should see the error.html page you just created, not the default Spring boot error page.

Create a custom error page is easy, but for most of the users, it’s not ideal because they want to separate error handling page for different errors. Let’s take a look at different solutions.

Spring boot exception @exceptionhandler example prep

Spring boot exception @exceptionhandler example prep

Let’s reuse the project we have created. Adding a new method in the controller.

@ExceptionHandler(value={java.lang.ArithmeticException.class})
public ModelAndView handlerArithmeticException(Exception e) {
   ModelAndView modelAndView = new ModelAndView();
   modelAndView.addObject("exception", e.toString());
   modelAndView.setViewName("mathError");
   return modelAndView;
}

Rename error.html to error.bak.html

Add new html under template folder named mathError.html.

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Title</title>
</head>
<body>
This contains math exceptions
</body>
</html>

035ND C01 L04 A04 EXCEPTION HANDLER

Spring boot - ExceptionHandler example 2

Spring boot - exceptionhandler example 2

This time we are going to handle a different exception, null pointer exception. Update the controller and add two methods

@RequestMapping("/update")
public String update() {
   String name = null;
   name = name.toLowerCase(); // this should cause null pointer exception
   return "update";
}

And

@ExceptionHandler(value={java.lang.NullPointerException.class})
public ModelAndView handlerNullPointerException(Exception e) {
   ModelAndView modelAndView = new ModelAndView();
   modelAndView.addObject("exception", e.toString());
   modelAndView.setViewName("nullPointerError");
   return modelAndView;
}

Add a new html page named nullPointerError.html under templates directory.

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Title</title>
</head>
<body>
This contains null pointer exceptions.
</body>
</html>